package com.weihe.toolkit.ids;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class IDGenerator {

	public static String newUUIDOf32() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}

	public static String newMD5Of32() {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			UUID uuid = UUID.randomUUID();
			String guidStr = uuid.toString();
			md.update(guidStr.getBytes(), 0, guidStr.length());
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	
	public static String newIDOf40() {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			UUID uuid = UUID.randomUUID();
			String guidStr = uuid.toString();
			md.update(guidStr.getBytes(), 0, guidStr.length());
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	
	
	public static void main(String[] args){
		System.out.println(IDGenerator.newIDOf40());
		System.out.println(IDGenerator.newMD5Of32());
		System.out.println(IDGenerator.newUUIDOf32());
	}
}
